package com.maltys.tree;

import com.maltys.tree.base.TreeNode;

/**
 * @author malty
 * @link <a href="https://leetcode.cn/problems/minimum-absolute-difference-in-bst/">...</a>
 */
public class MinimumDifferenceInBst {
    private Integer pre;
    private Integer minDiff = Integer.MAX_VALUE;

    public int getMinimumDifference(TreeNode root) {
        traverse(root);
        return minDiff;
    }

    private void traverse(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left != null) {
            traverse(root.left);
        }
        if (pre != null) {
            minDiff = Math.min(minDiff, Math.abs(root.val - pre));
        }
        pre = root.val;
        if (root.right != null) {
            traverse(root.right);
        }
    }
}
